<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
    <script type="text/javascript" src="http://www.broofa.com/Tools/Math.uuid.js"></script>
</head>
<body>
<div class="test">
    <label class="">地区</label>
    <div class="">
        <select id="select">
            <option value="0">请选择</option>
            <option class="opt" th:text="${HNS}" th:value="${HNS}"></option>
            <option class="opt" th:text="${GDS}" th:value="${GDS}"></option>
            <option class="opt" th:text="${BJS}" th:value="${BJS}"></option>
            <option class="opt" th:text="${FJS}" th:value="${FJS}"></option>
        </select>
    </div>
</div>
</body>

<script type="text/javascript">


    /*var ws = new WebSocket("ws://localhost:8080/buy?jspCode=AA");
    ws.onopen = function () {
        console.log("open");
        ws.send("hello");
    };
    ws.onmessage = function (evt) {
        console.log(evt.data)
    };
    ws.onclose = function (evt) {
        console.log("WebSocketClosed!");
    };
    ws.onerror = function (evt) {
        console.log("WebSocketError!");
    };*/


    $(function () {

        var c = $.cookie('create_order_cookie');
        if(c=='' || c==undefined){
            var uuid = Math.uuid();
            $.cookie('create_order_cookie', uuid, { expires: 7 });
        }else{
            var websocket;

            // 首先判断是否 支持 WebSocket
            if('WebSocket' in window) {

                websocket = new WebSocket("ws://localhost:8082/wsco");
            } else if('MozWebSocket' in window) {
                websocket = new MozWebSocket("ws://localhost:8082/wsco");
            } else {
                websocket = new SockJS("ws://localhost:8082/wsco");
            }

            // 打开连接时
            websocket.onopen = function(event) {
                console.log(" websocket.onopen  ");
            };

            // 收到消息时
            websocket.onmessage = function(event) {
                console.log("收到一条消息"+event.data);
                alert(event.data);
            };

            websocket.onerror = function(event) {
                console.log("  websocket.onerror  ");
            };

            websocket.onclose = function(event) {
                console.log("  websocket.onclose  ");
            };

            var message = {
                action: "register",
                createUser:c
            };

            websocket.addEventListener('open', function () {
                websocket.send(JSON.stringify(message))
            });
            $.get("/buy",function (e) {
                $('.test').append(e)
            });
        }
    },)

</script>

</html>